package com.zf.seata.seataorder.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zf.seata.seataorder.entity.ProdOrder;
import com.zf.seata.seataorder.feign.StorageFeign;
import com.zf.seata.seataorder.service.ProdOrderService;
import com.zf.seata.seataorder.mapper.ProdOrderMapper;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * order 调用 storage
 */
@Service
public class ProdOrderServiceImpl extends ServiceImpl<ProdOrderMapper, ProdOrder> implements ProdOrderService{
    @Autowired
    private StorageFeign storageFeign;

    /**
     * GlobalTransactional 是 seata 提供的分布式事务注解
     */
    @GlobalTransactional(rollbackFor = Exception.class)
    @Override
    public void testSeata() throws Throwable {
        ProdOrder order = new ProdOrder();
        order.setCount(12);
        order.setProductId(1L);
        order.setMoney(122L);
        this.save(order);
        storageFeign.changeStorage(1L, 93);

        //抛出异常, storage 服务 和 order 服务都会回滚
        System.out.println(1/0);
    }
}




